<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>首页</title>
  <!-- 切换选项卡时触发 visibilitychange 事件，来检测用户何时离开或进入网页 -->
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    document.addEventListener("visibilitychange", () => {
      console.log("document.hidden", document.hidden)
    });
  };
</script>

</html>